package friend

import (
	"api/internal/svc"
	"api/internal/types"
	"api/pb/friend"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetApplyListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetApplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApplyListLogic {
	return &GetApplyListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetApplyList 获取好友申请列表
func (l *GetApplyListLogic) GetApplyList(req *types.ApplyInfoListRequest) (resp *types.ApplyInfoListResponse, err error) {
	if req.ToUserID == 0 && req.FromUserID == 0 {
		return nil, errors.New("to_user_id or from_user_id is required")
	}

	result, err := l.svcCtx.FriendRPC.GetApplyList(l.ctx,
		&friend.ApplyListRequest{FromUserId: req.FromUserID, ToUserId: req.ToUserID, Offset: req.Offset, Limit: req.Limit})
	if err != nil {
		return nil, err
	}

	resp = &types.ApplyInfoListResponse{
		Total: result.Total,
		Data:  make([]types.ApplyInfoRequest, 0),
	}
	for _, item := range result.Data {
		resp.Data = append(resp.Data, types.ApplyInfoRequest{
			ID:         item.Id,
			FromUserID: item.FromUserId,
			ToUserID:   item.ToUserId,
			Message:    item.Message,
			Status:     item.Status,
		})
	}
	return resp, nil
}
